<template>
  <page title="部门管理">
    <el-row justify="space-between">
      <el-col :span="3">
        <el-popover
            trigger="click"
            width="200px"
            v-model:visible="visible">
          <template #reference>
            <el-button type="primary">新增部门</el-button>
          </template>
          <el-form>
            <el-form-item label="部门名:">
              <el-input size="mini" v-model="partName"></el-input>
            </el-form-item>
          </el-form>
          <div style="text-align: right;margin-top: 10px">
            <el-button type="primary" size="mini" @click="addPart">保存</el-button>
            <el-button size="mini" @click="visible = false">取消</el-button>
          </div>
        </el-popover>
      </el-col>
      <el-col :span="5">
        <el-form>
          <el-form-item label="部门名">
            <el-input prefix-icon="el-icon-search" v-model="searchKey"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table :data="table"  header-cell-class-name="table-header">
      <el-table-column label="部门名" prop="name">
      </el-table-column>
      <el-table-column label="用户数" prop="userCount">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="scope">
          <el-button
              type="text"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
              type="text"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </page>
</template>

<script>
import Page from "@/components/Page";
import axios from "axios";
export default {
  name: "PartManage",
  components: {Page},
  data(){
    return{
      tableData:[{
        name:'社会培训学院',
        userCount:3
      },{
        name:'教育学院',
        userCount:4
      },{
        name:'办公室',
        userCount:5
      },{
        name:'总务处',
        userCount:3
      },{
        name:'垃圾分类学院',
        userCount:6
      }],
      searchKey:'',

      visible:false,
      partName:'',
    }
  },
  methods:{
    addPart(){
      //console.log(this.partName);
      this.insertPart({id:0,name:this.partName,userCount:0})
      this.visible = false;
      this.partName = '';
    },
    handleEdit(index,row){
      console.log(index);
      this.$prompt('新名称', '重命名', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        row.name = value;
        this.updatePart(row);
      });
    },
    handleDelete(index,row){
      console.log(index);
      this.$confirm('确认删除此部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deletePart(row.id);
      });
    },
    findAllPart(){
      axios.get(this.$host+"/part").then(response =>{
        if(response.data.code === 0){
          this.tableData = response.data.data;
        }
      })
    },
    insertPart(data){
      axios.post(this.$host+"/part",data).then(response =>{
        if(response.data.code === 0){
          this.$message.success("新增成功")
          this.findAllPart();
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    updatePart(data){
      axios.put(this.$host+"/part",data).then(response =>{
        if(response.data.code === 0){
          this.$message.success("修改成功")
          this.findAllPart()
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    deletePart(id){
      axios.delete(this.$host+"/part/"+id).then(response =>{
        if(response.data.code === 0){
          this.$message.success("删除成功")
          this.findAllPart()
        }else{
          this.$message.error(response.data.msg);
        }
      })
    }
  },
  created() {
    this.findAllPart();
  },
  computed:{
    table(){
      return this.tableData.filter((value => {
        return this.searchKey === ''|| value.name.indexOf(this.searchKey) !== -1;
      }));
    }
  }
}
</script>

<style scoped>

</style>